package cn.lanqiao.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.lanqiao.entity.ApiResult;
import cn.lanqiao.entity.Pages;
import cn.lanqiao.entity.Student;
import cn.lanqiao.util.ApiResultHandler;

@CrossOrigin
@RequestMapping("/api/student")
@RestController
public class StudentController {

	@Autowired
	private cn.lanqiao.service.StudentService studentService;
	
	
	//分页
	@GetMapping(value="/studentpage")
	public Pages<Student> pager(
			@RequestParam(value="pageindex") int pageindex,
			@RequestParam(value="pagesize") int pagesize,
			@RequestParam(value="searchkey",required=false,defaultValue = "") String searchkey){
		return studentService.studentPage(pageindex, pagesize,searchkey);
	}
	
	//获取单条数据
	@ GetMapping("/get/{studentId}")
	public Map<String,Object> get(@PathVariable (value="studentId") Integer studentId){
		Map<String,Object>  map = new HashMap<String,Object>();
		Student student = studentService.getStudentById(studentId);
		if(student!=null) {
			map.put("data",student);
			map.put("status", 200);
			map.put("statusText","获取数据成功 ");
		}else {
			map.put("data",null);
			map.put("status", 0);
			map.put("statusText","获取数据失败 ");
		}
		return map;
	}
	
	//更新
	@PutMapping("/update")
	public Map<String , Object > update( @RequestBody  Student student ){
		int result = studentService.updateStudent(student);
		Map<String,Object>  map = new HashMap<String,Object>();
		if( result >0 ) {
			map.put("status", 200);
			map.put("statusText", "更新成功");
		} else {
			map.put("status", 0);
			map.put("statusText", "更新失败");
		}
		return map;
	}
	
	//删除
	@DeleteMapping("/delete/{studentId}")
	public Map<String,Object> delete(@PathVariable (value="studentId") Integer studentId){
		Map<String,Object>  map = new HashMap<String,Object>();
		int result = studentService.deleteStudent(studentId);
		if(result>0) {
			map.put("status", 200);
			map.put("statusText", "删除成功");
		}else{
			map.put("status", 0);
			map.put("statusText", "删除失败");
		}
		return map;
	}
		
	//添加
	@PostMapping("/insert")
	public Map<String,Object> insert(@RequestBody Student student){
		Map<String,Object>  map = new HashMap<String,Object>();
		int result = studentService.insertStudent(student);
		if(result>0) {
			map.put("status", 200);
			map.put("statusText", "添加成功");
		}else{
			map.put("status", 0);
			map.put("statusText", "添加失败");
		}
		return map;
	}		
		
	@PostMapping("/add")
    public ApiResult add(@RequestBody Student student) {
        int res = studentService.add(student);
        System.out.println(res);
        if (res == 1) {
            return ApiResultHandler.buildApiResult(200,"添加成功",null);
        }else {
            return ApiResultHandler.buildApiResult(400,"添加失败",null);
        }
    }
	@RequestMapping("/students/{page}/{size}")
    public ApiResult findAll(@PathVariable Integer page, @PathVariable Integer size) {
        Page<Student> studentPage = new Page<>(page,size);
        IPage<Student> res = studentService.PageStudent(studentPage);
        return  ApiResultHandler.buildApiResult(200,"分页查询所有学生",res);
    }

    @GetMapping("/student/{studentId}")
    public ApiResult findById(@PathVariable("studentId") Integer studentId) {
        Student res = studentService.findById(studentId);
        if (res != null) {
        return ApiResultHandler.buildApiResult(200,"请求成功",res);
        } else {
            return ApiResultHandler.buildApiResult(404,"查询的用户不存在",null);
        }
    }

    @DeleteMapping("/remove/{studentId}")
    public ApiResult deleteById(@PathVariable("studentId") Integer studentId) {
        return ApiResultHandler.buildApiResult(200,"删除成功",studentService.deleteById(studentId));
    }

    @RequestMapping("/studentPWD")
    public ApiResult updatePwd(@RequestBody Student student) {
        studentService.updatePwd(student);
        return ApiResultHandler.buildApiResult(200,"密码更新成功",null);
    }
	
	
}
